%% @author Hee Yeon Cho <aposto@nexon.co.kr>
%% @copyright 2009 Nexon Corp.

%% @doc 
%%      
	
-module (savage_tcp_listener).
-author('aposto@nexon.co.kr').

-export([start/1, stop/0 ]).
 
-include("engine.hrl").

%% External API

%% @spec test() -> ok
%% @doc Run all of the tests.
start(Port) ->	
	Sock = Port,	
	%Pid = spawn_link( fun() -> init(Parent, Sock) end ),
	Parent = self(),
	Pid = proc_lib:spawn_link( fun() -> init(Parent, Sock) end ),
	%Pid = proc_lib:start_link( ?MODULE, init, [Parent, Sock] ),
	
	io:format("#start tcp listener ~p:~p~n", [Port,Pid]),
  	{ok,Pid}.
	
stop() ->	
	Reply = lib:sendw(?MODULE, stop),
	io:format("#stop tcp listener ~p~n", [Reply]).
	
	 
init(Parent, ListenSock) ->
	proc_lib:init_ack(Parent, {ok, self()}),
	loop(ListenSock).
	
	 	 
loop(ListenSock) ->
	 
	receive 
		{accept, Sock} ->			
			%lib:send(?SESSION_MAN, {new, Sock}),			
			savage_sessionman:new_connection(S0ck),
			loop(ListenSock);
		{Pid, stop} ->			
			io:format("#close listener socket ~p:~p~n", [?MODULE, ListenSock] ),
			Pid ! stopped		
  	end.

 
	
%% Internal API
